/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmGasto.java
 *
 * Created on 10-oct-2011, 9:27:47
 */
package apiformularios.form;

import api.dom.Camion;
import api.dom.Gasto;
import api.dom.Utilitaria;
import api.gest.GestGastos;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class FrmGasto extends javax.swing.JInternalFrame {

    /** Creates new form FrmGasto */
    public FrmGasto() {

        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        initComponents();
        DefaultTableModel modelo = (DefaultTableModel) this.jTable2.getModel();
        Utilitaria.listarCamionesCombo((DefaultComboBoxModel) cCamiones.getModel());
        Utilitaria.listarGastos((DefaultTableModel) this.jTable2.getModel());
        this.jLabel8.setVisible(false);
        this.labellitros.setVisible(false);
        this.jLabel7.setVisible(false);
        this.jTextField1.setVisible(false);
        this.btnEliminar.setEnabled(false);
        cCamiones.setSelectedItem(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtimporte = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        cCamiones = new javax.swing.JComboBox();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        btnBuscar = new javax.swing.JButton();
        txtNombre1 = new javax.swing.JTextField();
        btnAgregar1 = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jcomborubro = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        labellitros = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setNextFocusableComponent(jDateChooser1);
        setNormalBounds(new java.awt.Rectangle(0, 0, 650, 570));
        setPreferredSize(new java.awt.Dimension(650, 570));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Fecha :");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Rubro: ");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 80, -1, 20));

        txtimporte.setNextFocusableComponent(jTextField1);
        txtimporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtimporteActionPerformed(evt);
            }
        });
        txtimporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtimporteKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtimporteKeyTyped(evt);
            }
        });
        getContentPane().add(txtimporte, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 80, 90, 20));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("Camion :");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero", "Fecha", "Camion", "Rubro", "Importe", "Observaciones"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTable2);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 630, 350));

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setPreferredSize(new java.awt.Dimension(650, 71));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel13.setBackground(new java.awt.Color(51, 255, 51));
        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel13.setText("Gastos");
        jLabel13.setToolTipText("");
        jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 49));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 70));

        cCamiones.setNextFocusableComponent(txtimporte);
        getContentPane().add(cCamiones, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 110, 330, -1));

        jDateChooser1.setNextFocusableComponent(jcomborubro);
        getContentPane().add(jDateChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 80, 140, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        jPanel2.add(btnBuscar, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 3, 70, 30));
        jPanel2.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 150, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 520, 270, 40));

        btnAgregar1.setText("Agregar");
        btnAgregar1.setNextFocusableComponent(btnEliminar);
        btnAgregar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 520, 80, 40));

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 520, 80, 40));

        jLabel15.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel15.setText("*");
        getContentPane().add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 20, -1));

        jLabel16.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel16.setText("Campos Obligatorios (*)");
        getContentPane().add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 140, 140, -1));

        jLabel17.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel17.setText("*");
        getContentPane().add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(630, 80, 20, -1));

        jcomborubro.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Viatico", "GasOil", "Lubricante", "Cubiertas", "Amorizacion", "Varios", "Seguro", "Taller" }));
        jcomborubro.setNextFocusableComponent(cCamiones);
        jcomborubro.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jcomborubroMouseClicked(evt);
            }
        });
        jcomborubro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcomborubroActionPerformed(evt);
            }
        });
        getContentPane().add(jcomborubro, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 80, 130, -1));

        jLabel18.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel18.setText("*");
        getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 110, 20, -1));

        jLabel19.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel19.setText("*");
        getContentPane().add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 80, 20, -1));

        labellitros.setFont(new java.awt.Font("Tahoma", 0, 12));
        labellitros.setText("..");
        getContentPane().add(labellitros, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 140, -1, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Importe:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 80, -1, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Litros: ");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 110, -1, -1));

        jTextField1.setNextFocusableComponent(btnAgregar1);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 110, 100, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Litros Aprox: ");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 140, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAgregar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar1ActionPerformed
// TODO add your handling code here:
    double litros = 0;
    try {
        if (jcomborubro.getSelectedItem().toString().equals("GasOil")) {
            if ("".equals(jTextField1.getText())) {
                litros = Double.parseDouble(labellitros.getText());
            } else {
                litros = Double.parseDouble(jTextField1.getText());
            }
        } else {
            litros = 0;
        }
        DefaultTableModel modelo = (DefaultTableModel) this.jTable2.getModel();
        String fecha = Utilitaria.convertirDateString(this.jDateChooser1.getDate());
        Camion objCamion = (Camion) this.cCamiones.getSelectedItem();
        Gasto objGasto = new Gasto(GestGastos.getInstance().devolverUltimoNum(), fecha, this.jcomborubro.getSelectedItem().toString(), Double.parseDouble(this.txtimporte.getText()), litros, objCamion);
        GestGastos.getInstance().altaGasto(objGasto);
        Utilitaria.listarGastos((DefaultTableModel) this.jTable2.getModel());
        //limpiar
        this.txtimporte.setText("");
        this.txtNombre1.setText("");
        this.jTextField1.setText("");
        Date hoy = new Date();
        this.jDateChooser1.setDate(hoy);
        cCamiones.setSelectedItem(null);
        JOptionPane.showMessageDialog(null, "Gasto agregado exsitosamente");
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(cCamiones, "Falto ingresar algun dato");
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(cCamiones, "Falto ingresar algun dato o son incorrectos");
    }
}//GEN-LAST:event_btnAgregar1ActionPerformed

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
// TODO add your handling code here:
    String s = String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0));
    int numero = Integer.parseInt(s);
    Gasto objGasto = GestGastos.getInstance().devolverGastoNumero(numero);
    this.btnEliminar.setEnabled(true);

    //this.txtAmortiz.setText(String.valueOf(objGasto.getgAmortizacion()));
    //this.txtCubierta.setText(String.valueOf(objGasto.getgCubierta()));
    // this.txtGasoil.setText(String.valueOf(objGasto.getgGasoil()));

    //this.txtLubricante.setText(String.valueOf(objGasto.getgLubricante()));
    //this.jDateChooser1.setDate(Utilitaria.convertirStringDate(objGasto.getgFecha()));
    //this.txtSeguro.setText(String.valueOf(objGasto.getgSeguro()));
    //this.txtSueldo.setText(String.valueOf(objGasto.getgSueldo()));
    //this.txtTaller.setText(String.valueOf(objGasto.getgTaller()));
    //this.txtVarios.setText(String.valueOf(objGasto.getgVarios()));
    //this.txtViatico.setText(String.valueOf(objGasto.getgViatico()));
    // this.cCamiones.setSelectedItem(GestCamiones.getInstance().devolverCamionNumero(objGasto.getObjCamion().getcMatricula()));
}//GEN-LAST:event_jTable2MouseClicked

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        try {
            if ("".equals(txtNombre1.getText())) {
                JOptionPane.showMessageDialog(rootPane, "Ingrese numero de gasto a buscar");
            } else {
                DefaultTableModel modelo32 = (DefaultTableModel) jTable2.getModel();
                modelo32.setRowCount(0);
                Gasto objG = GestGastos.getInstance().devolverGasto(Integer.parseInt(txtNombre1.getText()));
                Object[] objFa = {objG.getgNumero(), objG.getgFecha(), objG.getObjCamion().getcMatricula(), objG.getgRubro(), objG.getgImporte()};
                modelo32.addRow(objFa);
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(null, "No exsiste tal Gasto");
            txtNombre1.setText("");
            this.jTable2.setModel(Utilitaria.listarGastos((DefaultTableModel) jTable2.getModel()));
        }

    }//GEN-LAST:event_btnBuscarActionPerformed

    private void txtimporteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtimporteKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtimporteKeyTyped

    private void jcomborubroMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcomborubroMouseClicked
        // TODO add your handling code here:
        if (jcomborubro.getSelectedItem().toString().equals("GasOil")) {
            this.jLabel8.setVisible(true);
            this.labellitros.setVisible(true);
            this.jLabel7.setVisible(true);
            this.jTextField1.setVisible(true);
        }


    }//GEN-LAST:event_jcomborubroMouseClicked

    private void txtimporteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtimporteKeyReleased
        // TODO add your handling code here:
        try {
            if ("GasOil".equals(jcomborubro.getSelectedItem().toString())) {
                if ("".equals(txtimporte.getText())) {
                    labellitros.setText("0");
                } else {
                    double x1 = Utilitaria.calcularLitros(Double.parseDouble(txtimporte.getText()));
                    double asd = (double)Math.round(x1*100)/100;
                    System.out.println(asd);
                    /*NumberFormat NF = NumberFormat.getInstance();
                    NF.setMaximumFractionDigits(2); //2 decimales   */                   
                    String x = String.valueOf(asd);               
                    labellitros.setText(x);
                }
            }
        } catch (NumberFormatException e) {
        } catch (NullPointerException ex) {
        }
    }//GEN-LAST:event_txtimporteKeyReleased

    private void jcomborubroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcomborubroActionPerformed
        // TODO add your handling code here:
        if (jcomborubro.getSelectedItem().toString().equals("GasOil")) {
            this.jLabel8.setVisible(true);
            this.labellitros.setVisible(true);
             this.jLabel7.setVisible(true);
            this.jLabel7.setText("Litros: ");
            this.jTextField1.setVisible(true);
        } else {
            this.jLabel8.setVisible(false);
            this.labellitros.setVisible(false);
            this.jLabel7.setVisible(false);
            this.jTextField1.setVisible(false);
        }
    }//GEN-LAST:event_jcomborubroActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_jTextField1KeyTyped

private void txtimporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtimporteActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtimporteActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
    try {
        if (jTable2.getSelectedRow() < 0) {
            JOptionPane.showMessageDialog(null, "Seleccione un pago");
        } else {
            String x = (String.valueOf(this.jTable2.getValueAt(jTable2.getSelectedRow(), 0)));
            int numero = Integer.parseInt(x);
            GestGastos.getInstance().bajaGasto(numero);
            Utilitaria.limpiarJtables((DefaultTableModel) jTable2.getModel());
            Utilitaria.listarGastos((DefaultTableModel) this.jTable2.getModel());
            this.setEnabled(false);
        }
    } catch (Exception e) {
    }

}//GEN-LAST:event_btnEliminarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar1;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JComboBox cCamiones;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JComboBox jcomborubro;
    private javax.swing.JLabel labellitros;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextField txtimporte;
    // End of variables declaration//GEN-END:variables
}
